package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.dto.UnLikesBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Autowired
    IApBehaviorEntryService entryService;


    /**
     * 是否不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveUnLikes(UnLikesBehaviorDto dto) {


        EntryDto entryDto = new EntryDto();
        entryDto.setUserId(dto.getUserId());
        entryDto.setEquipmentId(dto.getEquipmentId());

        ApBehaviorEntry entry = entryService.getEntry(entryDto);

        ApUnlikesBehavior unlikesBehavior = new ApUnlikesBehavior();

        unlikesBehavior.setArticleId(Long.valueOf(dto.getArticleId()));
        unlikesBehavior.setCreatedTime(new Date());
        unlikesBehavior.setEntryId(Integer.valueOf(entry.getEntryId()));
        unlikesBehavior.setType(dto.getType());

        LambdaQueryWrapper<ApUnlikesBehavior> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());

        ApUnlikesBehavior one = this.getOne(wrapper);

        if (one==null) {


                this.save(unlikesBehavior);
        }else {
            if (one.getType()!=dto.getType()) {
                one.setType(dto.getType());
                this.updateById(one);
            }
        }



        return ResponseResult.okResult();
    }
}
